package automatyzacja;

import static org.junit.Assert.assertTrue;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Function;

public abstract class Page {

	protected WebDriver driver;

	public Page(WebDriver driver) {
		this.driver = driver;
	}

	protected void assertTextPresent(String text) {
		assertTrue(driver.getPageSource().contains(text));
	}

	protected void insertText(By ID, String Text) {
		WebElement findElement = driver.findElement(ID);
		findElement.sendKeys(Text);
	}

	protected void waitForElement(final By identifier, final String Text) {
		WebDriverWait wait = new WebDriverWait(driver, 10);

		wait.until(new Function<WebDriver, Boolean>() {
			@Override
			public Boolean apply(WebDriver d) {
				return d.findElement(identifier).getText().contains(Text);
			};
		});
	}

	protected void click(By ID) {
		driver.findElement(ID).click();

	}

	protected void open_www(String www) {
		driver.get(www);
	}

	protected boolean isTextPresent(String text) {
		return driver.getPageSource().contains(text);

	}

	protected boolean isElementPresent(By ID) {
		try {
			driver.findElement(ID);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

}
